﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bus.Bll;
using Bus.Model;

public partial class admin_cusAdd : System.Web.UI.Page
{
    private tb_custom cusbll = new tb_custom();
    private tb_cost costbll = new tb_cost();
    private tb_log logbll = new tb_log();
    private tb_config config = new tb_config();
    private CustomModel cm = null;
    private CostModel cml = null;//乘客充值表
    private LogModel lm = null;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        if (cname.Text == string.Empty)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户名不能为空！');</script>);");
            return;
        }
        if (cpwd.Text.ToLower() != twopwd.Text.ToLower())
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('密码不一致！');</script>);");
            return;
        }
        if (sfz.Text ==string.Empty )
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('身份证不能为空！');</script>);");
            return;
        }
        bool flg = cusbll.Exists(sfz.Text.Trim());
        if (flg == false)
        {
            cm = new CustomModel();
            cm.cname = cname.Text.Trim();
            cm.password = twopwd.Text.Trim();
            if (nan.Checked == true)
                cm.sex = 0;
            else
                cm.sex = 1;
            cm.idnum = sfz.Text.Trim();
            if (chr.Checked == true)
                cm.ctype = 1;//成人
            if (lhr.Checked == true)
                cm.ctype = 0;//老人
            if (xs.Checked == true)
                cm.ctype = 2;//学生
            cm.cardnumber = cardnum.Text.Trim();
            cm.age = DateTime.Now.Year - int.Parse(sfz.Text.Trim().Substring(6, 4));//计算乘客的年龄
            Random rd = new Random((int)DateTime.Now.Ticks);
            string nm = (DateTime.Now.Ticks + ((long)rd.Next(1000, 10000))).ToString();//生成卡号的方法
            cm.Nmcrad = nm;
            cm.regdate = DateTime.Now;
            int f = cusbll.Add(cm);//当前乘客的id
            if (f > 0)
            {
                cml = new CostModel();
                cml.id = f;
                cml.m1 = "乘客注册时的信息";
                cml.money = Convert.ToDecimal("0.0");
                cml.costtime = DateTime.Now;
                //获取不同类型的配置id
                if (chr.Checked == true)
                    cml.configid = config.GetModelList("configname='" + tb_config.crstr + "'")[0].cid;
                if (lhr.Checked == true)
                    cml.configid = config.GetModelList("configname='" + tb_config.lrstr + "'")[0].cid;
                if (xs.Checked == true)
                    cml.configid = config.GetModelList("configname='" + tb_config.xsstr + "'")[0].cid;

                //cml.configid = new tb_config().GetModelList("configname='"++"'")[0].cid;
                int flag = costbll.Add(cml);
                lm = new LogModel();
                lm.userid = int.Parse(Session["userid"].ToString());
                lm.op = "插入乘客";
                lm.memo = "插入乘客" + cm.cname;
                lm.opdate = DateTime.Now;
                int logid = logbll.Add(lm);//加入日志记录

                if (f > 0 && flag > 0 && logid > 0)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加乘客成功！');</script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加乘客失败！');</script>");
                }
            }
            else
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加乘客失败！');</script>");

        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('身份证号为唯一键，不能重复！');</script>");
        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        cname.Text = string.Empty;
        cpwd.Text = string.Empty;
        twopwd.Text = string.Empty;
        nan.Checked = true;
        chr.Checked = true;
        sfz.Text = string.Empty;
        cardnum.Text = string.Empty;
    }
}
